<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.7
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############




if(get('reset')==3){

    if(Antz_IntelliForm::submitted('confirm-reset')){
        $pData[3] = array();
        $pData[4] = array();
        JFX::redirect($this->adminUrl.'/new-version/');
    }


    $formFields = array(
        array(
            'type' => 'submit',
            'name' => 'submitBtn',
            'label' => '&nbsp;',
            'value' => 'Reset File Updates'
        )
    );

    echo JFX::makeRapidForm($formFields, 'Confirm Reset', 'confirm-reset');

    return;
}

















$priorVersion = $VM->getCurrentVersion($pData[1]['package_id']);
$versionid = $DB->oneValue($CONFIG->dbprefix.'vms_versions', 'id', "package_id = '{$pData[1]['package_id']}' AND version = '{$priorVersion}'");

$allFiles = array();
$scanFiles = $pData[2];
$fileContents = array();

eval('$basePath = "'.$pData[1]['base_path'].'";');

$basePath = rtrim($basePath, '/');

foreach($scanFiles as $k=>$v){
    $filepath = str_replace($basePath.'/', '', $v);
    $scanFiles[$k] = $filepath;
    if(is_dir($v)){
        $fileContents[$filepath] = NULL;
    }else{
        $fileContents[$filepath] = file_get_contents($v);
    }
    $allFiles[$filepath] = $filepath;
}

$oFiles = $DB->fetchAll("SELECT * FROM {$CONFIG->dbprefix}vms_files WHERE package_id = '{$pData[1]['package_id']}' AND version_id = '{$versionid}' ORDER BY filename ASC");

$origFiles = array();
$delFiles = array();


foreach($oFiles as $k=>$v){
    if(!in_array($v['filename'], $scanFiles)){
        $delFiles[] = $v['filename'];
        if($v['file_type']==0){
            $fileContents[$v['filename']] = NULL;
        }else{
            $fileContents[$v['filename']] = '';
        }
        $origFiles[] = $v['filename'];
        $allFiles[$v['filename']] = $v['filename'];
    }else{
        $origFiles[] = $v['filename'];
        $allFiles[$v['filename']] = $v['filename'];
    }
}

ksort($allFiles);

$updates = $allFiles;
foreach($updates as $k=>$v){
    $updates[$k] = VMS_FileUpdates_GetAction($v, $fileContents[$v], $pData[1]['package_id'], $versionid, $origFiles, $delFiles);
}


// auto-apply updates
if((string) substr(serialize($pData[3]), 0, 255) != (string) substr(serialize($updates), 0, 255)) $redirect = true;
else $redirect = false;

$pData[3] = $updates;

if($redirect) JFX::redirect($this->adminUrl.'/new-version/');




echo '<table><tbody><tr><th>File Name</th><th>Action</th></tr>';
foreach($allFiles as $k=>$v){
    echo VMS_FileUpdates_BuildRow($v, $fileContents[$v], $pData[1]['package_id'], $versionid, $origFiles, $delFiles);
}
echo '</tbody></table>';












function VMS_FileUpdates_BuildRow($filename, $fileContents, $packageid, $versionid, $origFiles, $delFiles){
    $CONFIG = JFX::registry('config');
    $DB = JFX::registry('db');
    $VMS = JFX::registry('JFX_Module_Vms');

    $fileid = (int) $DB->oneValue($CONFIG->dbprefix.'vms_files', 'id', "package_id = '{$packageid}' AND version_id = '{$versionid}' AND filename = '{$filename}'");

    if($fileid > 0 && file_exists($VMS->dirname.'/files/'.$fileid.'.vmsf')){
        $origFileContent = file_get_contents($VMS->dirname.'/files/'.$fileid.'.vmsf');
    }else{
        $origFileContent = '';
    }

    $output = '<tr><td><div align="left">'.$filename.'</div></td><td>';

    if(!in_array($filename, $origFiles)){
        // new file
        $output .= 'Create ';
        if($fileContents===NULL) $output .= 'Folder';
        else $output .= 'File';
    }else if(in_array($filename, $delFiles)){
        // deleted file
        $output .= 'Delete';
    }else if($fileContents !== NULL && (md5($origFileContent) !== md5($fileContents))){
        // changed file
        $output .= 'Update';
    }else{
        // no changes!
        $output .= 'N/C';
    }

    $output .= '</td></tr>';

    return $output;


}


function VMS_FileUpdates_GetAction($filename, $fileContents, $packageid, $versionid, $origFiles, $delFiles){
    $CONFIG = JFX::registry('config');
    $DB = JFX::registry('db');
    $VMS = JFX::registry('JFX_Module_Vms');

    $retVal = array(
        'filename' => $filename,
        'file_contents' => $fileContents,
        'package_id' => $packageid,
        'version_id' => $versionid
    );

    if($fileContents === NULL){
        $retVal['type'] = 0;
    }else{
        $retVal['type'] = 1;
    }

    $fileid = (int) $DB->oneValue($CONFIG->dbprefix.'vms_files', 'id', "package_id = '{$packageid}' AND version_id = '{$versionid}' AND filename = '{$filename}'");

    if($fileid > 0 && file_exists($VMS->dirname.'/files/'.$fileid.'.vmsf')){
        $origFileContent = file_get_contents($VMS->dirname.'/files/'.$fileid.'.vmsf');
    }else{
        $origFileContent = '';
    }

    if(!in_array($filename, $origFiles)){
        $retVal['action'] = 2; // create file/folder
    }else if(in_array($filename, $delFiles)){
        $retVal['action'] = 3; // delete file/folder
    }else if(md5($origFileContent) !== md5($fileContents)){
        $retVal['action'] = 1; // update file
    }else{
        // no change
        $retVal['action'] = 0;
    }

    return $retVal;

}



